package com.huahai.cofig;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Redisson 配置类
 * @author huahai
 */
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonConfiguration {

    private String host;

    private String port;

    private String password;

    private int dataBase;

    @Bean
    public RedissonClient redissonClient(){
        // 1.创建配置
        Config config = new Config();
        String redisAddress = String.format("redis://%s:%s", host, port);
        config.useSingleServer()
                .setPassword(password)
                .setAddress(redisAddress)
                .setDatabase(dataBase);
        // 2.创建实例
        return Redisson.create(config);
    }

}
